String.prototype.Trim = function(){
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

String.prototype.trim = function(){
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
//畫面載入後其動預設的系統效果
$(document).ready(function(){
	setTableEffect('.table_effect');
	setInputTexts();
	setNumOnly('.numOnly');
	setHide('.hide');
	checkedOnly('.checkedOnly');
	checkAllsiblings('.checkAllsiblings','.checkedOnly');
	checkAll('.checkAll','.checkedOnly');
	clickAll('.clickAll','.checkedOnly');
	unclickAll('.unclickAll','.checkedOnly');
	setCHYear('.chYear');
	setMonth('.month');
	setResize('.resize');
	checkErrorMsg('.error-msg');
	showDate('.showDate');
});

function checkErrorMsg(name){
	if(isEmptyString($(name).text())){
		$(name).hide();
	}
}

function setResize(name){
	$(name).resizable({
			handles: "se",
			create: function(event, ui){
				$(this).css({'margin':'0px'});
			}
		});
}

function setCHYear(name){
	$(name).find("option").remove();
	var date=new Date();
	var year=date.getFullYear()-1911;
	var begin=year-10;
	var end=year+10;
	for(i=begin;i<=end;i=i+1){
		if(i<100){
			if(i==year){
				$(name).append($("<option></option>").
				attr({"value":"0"+i,"selected":true}).text(i));
			}else{
				$(name).append($("<option></option>").
				attr({"value":"0"+i}).text(i));
			}
		}else{
			if(i==year){
				$(name).append($("<option></option>").
				attr({"value":i,"selected":true}).text(i));
			}else{
				$(name).append($("<option></option>").
				attr({"value":i}).text(i));
			}
		}
	}
	$(name).css({'width':'50px'});
}

function setMonth(name){
	$(name).find("option").remove();
	var date=new Date();
	var m=date.getMonth()+1;
	for(i=1;i<=12;i++){
		if(i<10){
			if(i==m){
				$(name).append($("<option></option>").
				attr("value","0"+i).attr("selected",true).       
				text(i));
			}else{
				$(name).append($("<option></option>").
				attr("value","0"+i).           
				text(i));
			}
		}else{
			if(i==m){
				$(name).append($("<option></option>").
				attr("value",i).attr("selected",true).       
				text(i));
			}else{
				$(name).append($("<option></option>").
				attr("value",i).           
				text(i));
			}
		}
	}
	$(name).css({'width':'40px'});
}


function checkedOnly(name){
	$(name).click(function(e){
		$(this).attr("checked",true);
	});
}

function clickAll(name,not){
	$(name).toggle(function(e){
		$(this).val('全取消');
		$("[type=checkbox]:not("+not+")").attr("checked",true);
	},function(e){
		$(this).val('全選');
		$("[type=checkbox]:not("+not+")").attr("checked",false);
	});
}

function unclickAll(name,not){
	$(name).click(function(e){
		$("[type=checkbox]:not("+not+")").attr("checked",false);
	});
}

function checkAll(name,not){
	$(name).click(function(e){
		if($(this).attr("checked")){
			$("[type=checkbox]:not("+not+")").attr("checked",true);
		}else{
			$("[type=checkbox]:not("+not+")").attr("checked",false);
		}
	});
}


function checkAllsiblings(name,not){
	$(name).click(function(e){
		if($(this).attr("checked")){
			$(this).siblings("[type=checkbox]:not("+not+")").attr("checked",true);
		}else{
			$(this).siblings("[type=checkbox]:not("+not+")").attr("checked",false);
		}
	});
}
//啟動Table的特效
function setTableEffect(name){
	$(name).find('tbody tr:even').addClass('tr_even');
	$(name+' tbody tr').hover(function(){
		$(this).addClass('tr_in');
	},function(){
		$(this).removeClass('tr_in');
	});
	$(name+' thead th').mousedown(function(){
		$(this).addClass('th_down');
	}).mouseup(function(){
		$(this).removeClass('th_down');
	}).mouseout(function(){
		$(this).removeClass('th_down');
	});
}
//啟動input type=text的特效
function setInputTexts(){
	$('input[type=text],input[type=password],textarea').addClass('input-blur');
	$('input[type=text],input[type=password],textarea').focus(function(){
		$(this).addClass('input-focus');
	}).blur(function(){
		$(this).removeClass('input-focus');
	});
}
//啟動Number Only的檢查
function setNumOnly(name){
	var numOnlyTmp='0';
	$(name).focus(function(){
		var obj=$(this);
		if(isEmpty(obj)){
			numOnlyTmp='0';
		}else{
			numOnlyTmp=obj.val();
		}
		obj.val('');
	});
	$(name).blur(function(){
		var obj=$(this);
		if(isEmpty(obj)){
			obj.val(numOnlyTmp);
		}
	});
	$(name).keypress(function(event){
		return(numFormat(event));
	});
	$(name).each(function(){
		var obj=$(this);
		if(isEmpty(obj)){
			$(this).val(0);
		}
	});
	
}

function setHide(name){
	$(name).hide();
}

function openDialog(_title,_url,_width,_height,_onClose){
	$('<div>').dialog({
        autoOpen: true,
        modal: false,
        stack: false,
        width: _width,
        height: _height,
        title: _title,
        open: function(){
         $(this).load(_url);
         $(this).dialog("open"); 
         return true;
        }
	});
}

function openIFrameSModal(_url,_width,_height, _onClose){
	$.modal("<iframe src='"+_url+"' height='"+_height+"' width='"+_width+"' style='border:0'/>", {
		containerCss:{
			backgroundColor:"#fff",
			borderColor:"#fff",
			height:_height,
			padding:0,
			width:_width
		},
		overlayClose:true,
		onClose: _onClose
	});
}

function openSModal(_url,_width,_height, _onClose){
	$.get(_url,function(data){
		$.modal(data, {
			containerCss:{
				backgroundColor:"#fff",
				borderColor:"#fff",
				height:_height,
				padding:0,
				width:_width
			},
			overlayClose:false,
			onClose: _onClose
		});
	});
}

function simpleModel(_id,_width,_height, _onClose){
	$(_id).modal({
		containerCss:{
			backgroundColor:"#fff",
			borderColor:"#fff",
			height:_height,
			padding:0,
			width:_width
		},
		overlayClose:false,
		onClose: _onClose
	});
	$(_id).children("select").show();
}

function customModel(){
	$('.exit').click(function () {
		$.modal.close();
		$(this).unbind('click');
	});
	var inx=$('#simplemodal-container .simplemodal-wrap').width();
	var iny=$('#simplemodal-container .simplemodal-wrap').height();
	var winx=$(window).width();
	var winy=$(window).height();
	if((inx+100)>=winx){
		$('#simplemodal-container .simplemodal-wrap').width(winx-100);
	}
	if((iny+100)>=winy){
		$('#simplemodal-container .simplemodal-wrap').height(winy-100);
	}
	$('#simplemodal-container').addClass('draggable').draggable();
}

//變更URL
function change_url(url){
	window.location =url;
}

//檢查keyevent, 當輸入的是數子則回傳true.
function numFormat(e){
	var whichCode = (window.Event) ? e.which : e.keyCode;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	if (whichCode == 13) return true;  // Enter
	if (whichCode == 8) return true;  // Delete (Bug fixed)
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
}

function compare(date1, date2){
	_date1=date1.split("/");
	_date2=date2.split("/");
	newDate1=new Date(_date1[1]+" "+_date1[0]+", "+_date1[2]);
	newDate2=new Date(_date2[1]+" "+_date2[0]+", "+_date2[2]);
	if(newDate1>newDate2){
		return true;
	}else{
		return false;
	}
}

function compareDate(date1, date2){
	_date1=date1.split("/");
	newDate1=new Date(_date1[1]+" "+_date1[0]+", "+_date1[2]);
	if(newDate1>date2){
		return true;
	}else{
		return false;
	}
}

function compareString(text1,text2){
	return (text1.trim()==text2.trim());
}

function checkOption(obj,value){
	if (obj!=null && obj.options!=null) {
		for(i=0;i<obj.options.length;i++){
			if(obj.options[i].value==value){
				return true;
			}
		}
	}
	return false;
}

function isEmptyString(obj){
	if(obj){
		 if(obj.trim().length==0)
			return true;
		else 
			return false;
	}else{
		return true;
	}
}

function isEmpty(obj){
	if(obj){
		if(obj.val()){
			if(obj.val().trim().length==0)
				return true;
			else 
				return false;
		}else{
			return true;
		}
	}else{
		return true;
	}
}

function isInteger(val){
    var reg=/^[0-9]?\d+$/;  
    if(!reg.test(val)){
        return false;
    }else{
        return true;
    }
}

function showErrorMsg(msg){
	$('.error-msg').show().text(msg);
}

function showMsg(id,msg){
	if(msg==null||msg=='null'){
		msg='';
	}
	$(id+'Msg').text(msg);
}

function showText(id,msg){
	if(msg==null||msg=='null'){
		msg='';
	}
	$(id).val(msg);
}

function showAll(id,msg){
	showMsg(id,msg);
	showText(id,msg);
}

function focusClear(obj){
	obj.val('');
}

function blurClear(obj){
	if(isEmpty(obj)){
		obj.val('0');
	}
}

function setParent(id, value){
		opener.document.getElementById(id).value+="'"+value+"',";
}

function getParent(id){
	return opener.document.getElementById(id).value;
}

var openWinCallback;

function openWin(url, width, height, callback){
	var winy=screen.availHeight
	var winx=screen.availWidth;
	if(!width||width==0){
		width=600;
	}
	if(!height|| height==0){
		height=500;
	}
	if((width+100)>=winx){
		width=(winx-200);
	}
	if((height+100)>=winy){
		height=(winy-200);
	}
	WREF = window.open (url, 'newwindow', 'height='+height+', width='+width+', top='+(winy-height)/2+', left='+(winx-width)/2+', toolbar=no, menubar=no, scrollbars=yes, resizable=no,location=no, status=yes')
	if(!WREF.opener){ WREF.opener = this.window; }
	openWinCallback=callback;
	 if(WREF != null) {
      if(!WREF.closed) {
	    if(WREF.blur) {
	    	WREF.focus();
	     }
	   }
	}
}

function addOptions(name, object){
	$.each(object, function(key,value){
		$(name).append($("<option></option>").
		attr("value",key).           
		text(value));
	}); 
}

function addOptionsHtml(name, data){
	$(name).find("option").remove();
	$(name).append(data);
}

function showDate(name){
	$(name).attr({'readonly':true,'size':10,'maxlength':10});
	$(name).datepicker({dateFormat: 'yy/mm/dd',changeMonth: true,changeYear: true,showButtonPanel: true});
}

function round(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

//For IE6
function setSelected(id, value){
	try{
		$(id).val(value);
	}catch(ex) {
      setTimeout("$('" + id + "').val('" + value + "')",1);
   }

}


